package br.com.ufpr.sgp.pages;

import javax.ejb.EJB;

import org.apache.wicket.PageReference;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.validation.EqualInputValidator;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;
import org.wicketstuff.annotation.mount.MountPath;

import br.com.sebrae.pr.sgp.entities.Usuario;
import br.com.sebrae.pr.sgp.services.UsuarioServiceRemote;
import br.com.sgp.util.handling.ErrorHandlingAjaxButton;
import br.com.sgp.util.handling.ErrorHandlingForm;
import br.com.ufpr.sgp.util.session.CustomSession;
import br.com.ufpr.util.base.BasePage;
import br.com.ufpr.util.feedback.IReceiveFeedbackPage;


//@Protected
@MountPath("AlterarSenha")
public class AlterarSenhaPage extends BasePage {
    private static final long serialVersionUID = -1190106089036183289L;

    @EJB
    private UsuarioServiceRemote usuarioServiceRemote;

    private String senhaAtual;
    private String novaSenha;
    private String novaSenhaConf;

    public AlterarSenhaPage(final ModalAlterarSenha modalAlterarSenha, final PageReference pageReference) {
        super();

        final IModel<Usuario> usuModel = new Model<Usuario>(CustomSession.get().getUsuario());

        final ErrorHandlingForm<AlterarSenhaPage> senhaForm = new ErrorHandlingForm<AlterarSenhaPage>("senhaForm",
                new CompoundPropertyModel<AlterarSenhaPage>(this));

        final PasswordTextField senhaAtual = new PasswordTextField("senhaAtual");
        senhaAtual.setLabel(new ResourceModel("alterarSenha.senhaAtual"));
        senhaAtual.setRequired(true);
        senhaForm.add(senhaAtual);

        final PasswordTextField novaSenha = new PasswordTextField("novaSenha");
        novaSenha.setLabel(new ResourceModel("alterarSenha.novaSenha"));
        novaSenha.setRequired(true);
        senhaForm.add(novaSenha);

        final PasswordTextField novaSenhaConf = new PasswordTextField("novaSenhaConf");
        novaSenhaConf.setLabel(new ResourceModel("alterarSenha.novaSenhaConf"));
        novaSenhaConf.setRequired(true);
        senhaForm.add(novaSenhaConf);

        senhaForm.add(new EqualInputValidator(novaSenha, novaSenhaConf));

        senhaForm.add(new ErrorHandlingAjaxButton("btnVoltar") {
            private static final long serialVersionUID = -1021944529439462900L;

            @Override
            protected void onSubmitted(AjaxRequestTarget target) {
                if (modalAlterarSenha != null) {
                    modalAlterarSenha.close(target);

                } else {
                    target.appendJavaScript("window.history.back();");
                }
            }

        }.setDefaultFormProcessing(false));

        senhaForm.add(new ErrorHandlingAjaxButton("btnSalvar") {
            private static final long serialVersionUID = -7264681324748894683L;

            @Override
            protected void onSubmitted(AjaxRequestTarget target) {
                setUsuarioLogadoDto(usuModel);
                // TODO usuarioServiceRemote.alterarSenha(usuModel.getObject(), AlterarSenhaPage.this.senhaAtual,
                // AlterarSenhaPage.this.novaSenha);
                if (pageReference != null && pageReference.getPage() instanceof IReceiveFeedbackPage) {
                    final IReceiveFeedbackPage irfp = (IReceiveFeedbackPage) pageReference.getPage();
                    irfp.setFeedbackMessage(new ResourceModel("alterarSenha.update.success"), target);
                }
            }

            @Override
            protected void onSucess(AjaxRequestTarget target) {
                if (modalAlterarSenha != null) {
                    modalAlterarSenha.close(target);

                } else {
                    target.appendJavaScript("window.history.back();");
                }
            }

        });

        add(senhaForm);

    }

    public String getSenhaAtual() {
        return senhaAtual;
    }

    public void setSenhaAtual(String senhaAtual) {
        this.senhaAtual = senhaAtual;
    }

    public String getNovaSenha() {
        return novaSenha;
    }

    public void setNovaSenha(String novaSenha) {
        this.novaSenha = novaSenha;
    }

    public String getNovaSenhaConf() {
        return novaSenhaConf;
    }

    public void setNovaSenhaConf(String novaSenhaConf) {
        this.novaSenhaConf = novaSenhaConf;
    }
}
